package com.example.Map;

import java.util.*;

public class Test2Map {
    public static void main(String[] args) {
        //method();

        TreeMap<Student, String> tm = new TreeMap<>(
                (new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        int result = o2.getAge() - o1.getAge();
                        result = result == 0 ? o2.getName().compareTo(o1.getName()) : result;
                        return result;
                    }
                }
                ));

        Student s1 = new Student("xiaoshuai", 20);
        Student s2 = new Student("xiaomei", 20);
        Student s3 = new Student("xiaohei", 17);

        tm.put(s1, "江苏");
        tm.put(s2, "北京");
        tm.put(s3, "天津");

        tm.forEach(
                (Student key, String value) -> {
                    System.out.println(key + "---" + value);
                });
//        Student{name='xiaoshuai', age=20}---江苏
//        Student{name='xiaomei', age=20}---北京
//        Student{name='xiaohei', age=17}---天津

    }

    private static void method() {
        HashMap<Student, String> hm = new HashMap<>();

        Student s1 = new Student("xiaoshuai", 19);
        Student s2 = new Student("xiaomei", 20);
        Student s3 = new Student("xiaohei", 17);

        hm.put(s1, "江苏");
        hm.put(s2, "北京");
        hm.put(s3, "天津");

        Set<Student> keys = hm.keySet();
        for (Student key : keys) {
            String value = hm.get(key);
            System.out.println(key + "---" + value);
        }
        System.out.println("-------------------------------------");

        Set<Map.Entry<Student, String>> entries = hm.entrySet();
        for (Map.Entry<Student, String> entry : hm.entrySet()) {
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + "---" + value);
        }
        System.out.println("--------------------------------------");

        hm.forEach((Student key, String value) -> {
            System.out.println(key + "---" + value);
        });
//        Student{name='xiaomei', age=20}---北京
//        Student{name='xiaoshuai', age=19}---江苏
//        Student{name='xiaohei', age=17}---天津

    }
}

class Student /*implements Comparable<Student>*/ {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    /*@Override
    public int compareTo(Student o) {
        int result = o.getAge() - this.getAge();
        result = result == 0 ? o.getName().compareTo(this.getName()) : result;
        return result;
    }*/
}